package com.softtown.modules.sys.service.impl;

import com.softtown.common.service.impl.BaseServiceImpl;
import com.softtown.common.utils.ConvertUtils;
import com.softtown.common.utils.HttpContextUtils;
import com.softtown.common.utils.TreeUtils;
import com.softtown.modules.security.user.SecurityUser;
import com.softtown.modules.security.user.UserDetail;
import com.softtown.modules.sys.dao.SysCompMenuDao;
import com.softtown.modules.sys.dao.SysGuestMenuDao;
import com.softtown.modules.sys.dao.SysMenuDao;
import com.softtown.modules.sys.dto.SysAppMenuDTO;
import com.softtown.modules.sys.dto.SysMenuDTO;
import com.softtown.modules.sys.entity.*;
import com.softtown.modules.sys.enums.MenuTypeEnum;
import com.softtown.modules.sys.service.*;
import com.softtown.modules.sys.vo.SysCompanyVO;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 公司角色用户
 *
 * @author Dingyayong
 * @version v1.0
 * @create 2019-05-20 17:41
 **/
@Service
public class SysCompMenuServiceImpl extends BaseServiceImpl<SysCompMenuDao, SysCompMenuEntity> implements SysCompMenuService {

    @Resource
    private CompSysRoleUserService compSysRoleUserService;

    @Resource
    private CompSysRoleMenuService compSysRoleMenuService;

    @Resource
    private SysCompService sysCompService;

    @Resource
    private SysMenuDao sysMenuDao;

    @Resource
    private SysGuestMenuDao sysGuestMenuDao;

    /**
     * 客户
     */
    private final String customer ="http://49.235.104.68:8801/aio-system/customer.png";


    /**
     * 资源池
     */
    private final String resource ="http://49.235.104.68:8801/aio-system/resource.png";


    /**
     * 报备
     */
    private final String report = "http://49.235.104.68:8801/aio-system/report.png";


    /**
     * 拜访记录
     */
    private final String visitRecord = "http://49.235.104.68:8801/aio-system/visitRecord.png";


    /**
     * 售后
     */
    private final String afterSale ="http://49.235.104.68:8801/aio-system/afterSale.png";


    /**
     * 维修
     */
    private final String service = "http://49.235.104.68:8801/aio-system/service.png";

    /**
     * 现存量
     */
    private final String accessRecord = "http://49.235.104.68:8801/aio-system/accessRecord.png";



    /**
     * 出入库
     */
    private final String existReport = "http://49.235.104.68:8801/aio-system/existReport.png";



    @Override
    public List<SysMenuDTO> getListApp(Long companyId) {
        // 该账户在这个公司的可以显示的APP
        List<SysMenuEntity> menuEntityList = new ArrayList<>();
        // 获取该公司的信息
        SysCompanyVO companyInfo = sysCompService.getCompanyInfo(companyId);
        UserDetail user = SecurityUser.getUser();
        // 初始化APP_LIST
        List<SysMenuEntity> compAppList = new ArrayList<>();
        // 获取当前日期（用于过滤APP的有效期）
        Date date = new Date();
        // 获取所有的APP_MENU
        List<SysMenuEntity> menuList = sysMenuDao.getMenuList(MenuTypeEnum.APP.value(), "zh-CN");
        Hashtable<Long, SysMenuEntity> sysMenuEntityHashtable = new Hashtable<>();
        for(SysMenuEntity sysMenuEntity: menuList) {
            sysMenuEntityHashtable.put(sysMenuEntity.getId(), sysMenuEntity);
        }
        // 判断是否是企业管理员
        // 获取公司的App
        if (companyInfo.getAdminUserId().equals(user.getId())) {
            // 过滤过期的APP
            List<SysCompMenuEntity> sysCompMenuEntityList = baseDao.getMenuAppListByCompanyId(companyId, MenuTypeEnum.APP.value());
            if (sysCompMenuEntityList.size() > 0) {
                List<SysCompMenuEntity> sysCompMenuEntities = sysCompMenuEntityList.stream()
                        .filter(sysCompMenuEntity -> sysCompMenuEntity.getExprDate().getTime() > date.getTime()).collect(Collectors.toList());
                // 找出公司的APP_MENU
                for (SysCompMenuEntity sysCompMenuEntity : sysCompMenuEntities) {
                    compAppList.add(sysMenuEntityHashtable.get(sysCompMenuEntity.getMenuId()));
                }
            }
        } else {
            // 非企业管理员APP
            // 获取公司所有的APP并过滤过期的APP
            List<SysCompMenuEntity> sysCompMenuEntityList = baseDao.getMenuAppListByCompanyId(companyId, MenuTypeEnum.APP.value());
            if (sysCompMenuEntityList.size() > 0) {
                Map<Long, SysCompMenuEntity> sysCompMenuEntityMap = sysCompMenuEntityList.stream()
                        .filter(sysCompMenuEntity -> sysCompMenuEntity.getExprDate().getTime() > date.getTime()).collect(Collectors.toMap(SysCompMenuEntity::getMenuId, a -> a,(k1,k2)->k1));
                // 获取公司用户菜单信息
                Long userId = SecurityUser.getUserId();
                List<Long>  roleIdList =compSysRoleUserService.getRoleIdList(companyId,userId);
                List<CompSysRoleMenuEntity> compSysRoleMenuEntityList = compSysRoleMenuService.getCompMenuByRoles(companyId,
                        roleIdList);
                // 通过角色用有的APP去公司APP中进行过滤（找出有效的APP）
                List<SysCompMenuEntity> validAppIds = new ArrayList<>();
                for (CompSysRoleMenuEntity compSysRoleMenuEntity : compSysRoleMenuEntityList) {
                    if (sysCompMenuEntityMap.get(compSysRoleMenuEntity.getMenuId()) != null) {
                        validAppIds.add(sysCompMenuEntityMap.get(compSysRoleMenuEntity.getMenuId()));
                    }
                }
                // 根据公司APP_ID获取
                for (SysCompMenuEntity sysCompMenuEntity : validAppIds) {
                    menuEntityList.add(sysMenuEntityHashtable.get(sysCompMenuEntity.getMenuId()));
                }
            }
        }
        // 获取游客的APP
        List<SysMenuEntity> guestMenus = sysGuestMenuDao.getGuestMenuList(MenuTypeEnum.APP.value(), HttpContextUtils.getLanguage());
        // 将游客APP和公司APP进行合并
        menuEntityList.addAll(compAppList);
        menuEntityList.addAll(guestMenus);
        List<SysMenuEntity> sysMenuEntities = new ArrayList<>(new LinkedHashSet<>(menuEntityList));
        //对集合进行排序
        sysMenuEntities.sort(new Comparator<SysMenuEntity>() {
            @Override
            public int compare(SysMenuEntity o1, SysMenuEntity o2) {
                if (o1.getSort() > o2.getSort()) {
                    return 1;
                }
                if (o1.getSort().equals(o2.getSort())) {
                    return 0;
                }
                return -1;
            }
        });
        //转换为DTO
        return ConvertUtils.sourceToTarget(sysMenuEntities, SysMenuDTO.class);
    }

    @Override
    public List<SysMenuDTO> getListTreeApp(Long companyId) {
        List<SysMenuEntity> menuList;
            menuList = sysMenuDao.getRoleMenuList(null,companyId, HttpContextUtils.getLanguage());
        List<SysMenuDTO> dtoList = ConvertUtils.sourceToTarget(menuList, SysMenuDTO.class);
        return TreeUtils.build(dtoList);
    }

    @Override
    public List<SysAppMenuDTO> getAppMenuList(Long companyId) {
        UserDetail user = SecurityUser.getUser();
        List<Long> roleIds = compSysRoleUserService.getRoleId(companyId, user.getId());
        List<SysAppMenuDTO> sysAppMenuDTOList = new ArrayList<>();
        List<Long> mainMenuList = new ArrayList<>();
        // 业务管理
        mainMenuList.add(1088611641903702176L);
        // 仓库管理
        mainMenuList.add(1088611641903702177L);
        // 报表管理
        mainMenuList.add(1088611641903702178L);

        List<Long> menuList = new ArrayList<>();
        // 客户管理
        menuList.add(1088611641903702180L);
        // 客户资源池
        menuList.add(1088611641903702205L);
        // 报备
        menuList.add(1088611641903702206L);
        // 拜访记录
        menuList.add(1088611641903702207L);
        // 售后
        menuList.add(1088611641903702187L);
        // 维修服务单
        menuList.add(1088611641903702188L);


        List<Long> menuList1 = new ArrayList<>();
        // 设备库存
        menuList1.add(1088611641903702195L);
        // 出库明细
        menuList1.add(1088611641903702198L);

        List<Long> menuList2 = new ArrayList<>();
        // 设备租赁记录
        menuList2.add(1088611641903702202L);
        // 销售发货记录
        menuList2.add(1088611641903702203L);
        // 销售记录
        menuList2.add(1088611641903702208L);

        List<SysMenuDTO> sysMenuDTOList = this.getListApp(companyId);
        mainMenuList.forEach(menuId ->{
            sysMenuDTOList.forEach(item ->{
                if (menuId.equals(1088611641903702176L) && menuId.equals(item.getId())){
                    SysAppMenuDTO sysAppMenuDTO = new SysAppMenuDTO();
                    sysAppMenuDTO.setId(menuId);
                    sysAppMenuDTO.setColor("#3196fb");
                    sysAppMenuDTO.setIcon(item.getIcon());
                    sysAppMenuDTO.setMenuName(item.getName());
                    sysAppMenuDTO.setSort(item.getSort());
                    List<SysMenuDTO> sysMenuDTOList1 = new ArrayList<>();
                    List<SysMenuDTO> sysMenuDTOS = this.getUserMenuLists(user.getId(),companyId,menuId,roleIds);
                    if(sysMenuDTOS.size() >0){
                        sysMenuDTOS.forEach(sysMenuDTO ->{
                            if(sysMenuDTO.getChildren().size() >0){
                                sysMenuDTOList1.addAll(sysMenuDTO.getChildren());
                            }
                        });
                    }
                    if(sysMenuDTOList1.size() >0 ){
                        List<SysAppMenuDTO> sysAppMenuDTOList1 = new ArrayList<>();
                        menuList.forEach(id ->{
                            sysMenuDTOList1.forEach(sysMenuDTO -> {
                                // 客户
                                if(id.equals(1088611641903702180L) && id.equals(sysMenuDTO.getId())){
                                    SysAppMenuDTO sysAppMenuDTO1 = new SysAppMenuDTO();
                                    sysAppMenuDTO1.setId(id);
                                    sysAppMenuDTO1.setUrl("/pages/Business/customInfo/index");
                                    sysAppMenuDTO1.setIcon(customer);
                                    sysAppMenuDTO1.setMenuName(sysMenuDTO.getName());
                                    sysAppMenuDTO1.setSort(sysMenuDTO.getSort());
                                    sysAppMenuDTOList1.add(sysAppMenuDTO1);
                                }
                                // 客户资源池
                                if(id.equals(1088611641903702205L) && id.equals(sysMenuDTO.getId())){
                                    SysAppMenuDTO sysAppMenuDTO1 = new SysAppMenuDTO();
                                    sysAppMenuDTO1.setId(id);
                                    sysAppMenuDTO1.setUrl("/pages/Business/customPotential/index");
                                    sysAppMenuDTO1.setIcon(resource);
                                    sysAppMenuDTO1.setMenuName(sysMenuDTO.getName());
                                    sysAppMenuDTO1.setSort(sysMenuDTO.getSort());
                                    sysAppMenuDTOList1.add(sysAppMenuDTO1);
                                }
                                // 报备
                                if(id.equals(1088611641903702206L) && id.equals(sysMenuDTO.getId())){
                                    SysAppMenuDTO sysAppMenuDTO1 = new SysAppMenuDTO();
                                    sysAppMenuDTO1.setId(id);
                                    sysAppMenuDTO1.setUrl("/pages/Business/report/index");
                                    sysAppMenuDTO1.setIcon(report);
                                    sysAppMenuDTO1.setMenuName(sysMenuDTO.getName());
                                    sysAppMenuDTO1.setSort(sysMenuDTO.getSort());
                                    sysAppMenuDTOList1.add(sysAppMenuDTO1);
                                }
                                // 拜访记录
                                if(id.equals(1088611641903702207L) && id.equals(sysMenuDTO.getId())){
                                    SysAppMenuDTO sysAppMenuDTO1 = new SysAppMenuDTO();
                                    sysAppMenuDTO1.setId(id);
                                    sysAppMenuDTO1.setUrl("/pages/Business/visitRecord/index");
                                    sysAppMenuDTO1.setIcon(visitRecord);
                                    sysAppMenuDTO1.setMenuName(sysMenuDTO.getName());
                                    sysAppMenuDTO1.setSort(sysMenuDTO.getSort());
                                    sysAppMenuDTOList1.add(sysAppMenuDTO1);
                                }

                                // 售后单
                                if(id.equals(1088611641903702187L) && id.equals(sysMenuDTO.getId())){
                                    SysAppMenuDTO sysAppMenuDTO1 = new SysAppMenuDTO();
                                    sysAppMenuDTO1.setId(id);
                                    sysAppMenuDTO1.setUrl("/pages/Business/afterSale/index");
                                    sysAppMenuDTO1.setIcon(afterSale);
                                    sysAppMenuDTO1.setMenuName(sysMenuDTO.getName());
                                    sysAppMenuDTO1.setSort(sysMenuDTO.getSort());
                                    sysAppMenuDTOList1.add(sysAppMenuDTO1);
                                }

                                // 维修服务
                                if(id.equals(1088611641903702188L) && id.equals(sysMenuDTO.getId())){
                                    SysAppMenuDTO sysAppMenuDTO1 = new SysAppMenuDTO();
                                    sysAppMenuDTO1.setId(id);
                                    sysAppMenuDTO1.setUrl("/pages/Business/serviceOrder/index");
                                    sysAppMenuDTO1.setIcon(service);
                                    sysAppMenuDTO1.setMenuName(sysMenuDTO.getName());
                                    sysAppMenuDTO1.setSort(sysMenuDTO.getSort());
                                    sysAppMenuDTOList1.add(sysAppMenuDTO1);
                                }

                            });
                        });
                        sysAppMenuDTO.setChildren(sysAppMenuDTOList1);
                    }
                    sysAppMenuDTOList.add(sysAppMenuDTO);

                }
                if (menuId.equals(1088611641903702177L) && menuId.equals(item.getId())){
                    SysAppMenuDTO sysAppMenuDTO = new SysAppMenuDTO();
                    sysAppMenuDTO.setId(menuId);
                    sysAppMenuDTO.setColor("#ffab15");
                    sysAppMenuDTO.setIcon(item.getIcon());
                    sysAppMenuDTO.setMenuName(item.getName());
                    sysAppMenuDTO.setSort(item.getSort());
                    List<SysMenuDTO> sysMenuDTOList1 = new ArrayList<>();
                    List<SysMenuDTO> sysMenuDTOS = this.getUserMenuLists(user.getId(),companyId,menuId,roleIds);
                    if(sysMenuDTOS.size() >0){
                        sysMenuDTOS.forEach(sysMenuDTO ->{
                            if(sysMenuDTO.getChildren().size() >0){
                                sysMenuDTOList1.addAll(sysMenuDTO.getChildren());
                            }
                        });
                    }
                    if(sysMenuDTOList1.size() >0 ){
                        List<SysAppMenuDTO> sysAppMenuDTOList1 = new ArrayList<>();
                        menuList1.forEach(id ->{
                            sysMenuDTOList1.forEach(sysMenuDTO -> {
                                if(id.equals(1088611641903702195L) && id.equals(sysMenuDTO.getId())){
                                    SysAppMenuDTO sysAppMenuDTO1 = new SysAppMenuDTO();
                                    sysAppMenuDTO1.setId(id);
                                    sysAppMenuDTO1.setUrl("/pages/Warehouse/existReportForm/index");
                                    sysAppMenuDTO1.setIcon(existReport);
                                    sysAppMenuDTO1.setMenuName(sysMenuDTO.getName());
                                    sysAppMenuDTO1.setSort(sysMenuDTO.getSort());
                                    sysAppMenuDTOList1.add(sysAppMenuDTO1);
                                }
                                if(id.equals(1088611641903702198L) && id.equals(sysMenuDTO.getId())){
                                    SysAppMenuDTO sysAppMenuDTO1 = new SysAppMenuDTO();
                                    sysAppMenuDTO1.setId(id);
                                    sysAppMenuDTO1.setUrl("/pages/Warehouse/accessRecord/index");
                                    sysAppMenuDTO1.setIcon(accessRecord);
                                    sysAppMenuDTO1.setMenuName(sysMenuDTO.getName());
                                    sysAppMenuDTO1.setSort(sysMenuDTO.getSort());
                                    sysAppMenuDTOList1.add(sysAppMenuDTO1);
                                }
                            });
                        });
                        sysAppMenuDTO.setChildren(sysAppMenuDTOList1);
                    }
                    sysAppMenuDTOList.add(sysAppMenuDTO);
                }
                if (menuId.equals(1088611641903702178L) && menuId.equals(item.getId())){
                    SysAppMenuDTO sysAppMenuDTO = new SysAppMenuDTO();
                    sysAppMenuDTO.setId(menuId);
                    sysAppMenuDTO.setColor("#808000");
                    sysAppMenuDTO.setIcon(item.getIcon());
                    sysAppMenuDTO.setSort(item.getSort());
                    sysAppMenuDTO.setMenuName(item.getName());
                    sysAppMenuDTO.setSort(item.getSort());
                    List<SysMenuDTO> sysMenuDTOList1 = new ArrayList<>();
                    List<SysMenuDTO> sysMenuDTOS = this.getUserMenuLists(user.getId(),companyId,menuId,roleIds);
                    if(sysMenuDTOS.size() >0){
                        sysMenuDTOS.forEach(sysMenuDTO ->{
                            if(sysMenuDTO.getChildren().size() >0){
                                sysMenuDTOList1.addAll(sysMenuDTO.getChildren());
                            }
                        });
                    }
                    if(sysMenuDTOList1.size() >0 ){
                        List<SysAppMenuDTO> sysAppMenuDTOList1 = new ArrayList<>();
                        menuList2.forEach(id ->{
                            sysMenuDTOList1.forEach(sysMenuDTO -> {
                                if(id.equals(1088611641903702202L) && id.equals(sysMenuDTO.getId())){
                                    SysAppMenuDTO sysAppMenuDTO1 = new SysAppMenuDTO();
                                    sysAppMenuDTO1.setId(id);
                                    sysAppMenuDTO1.setUrl("/pages/ReportForm/deviceRecord/index");
                                    sysAppMenuDTO1.setIcon(sysMenuDTO.getIcon());
                                    sysAppMenuDTO1.setMenuName(sysMenuDTO.getName());
                                    sysAppMenuDTO1.setSort(sysMenuDTO.getSort());
                                    sysAppMenuDTOList1.add(sysAppMenuDTO1);
                                }
                                if(id.equals(1088611641903702203L) && id.equals(sysMenuDTO.getId())){
                                    SysAppMenuDTO sysAppMenuDTO1 = new SysAppMenuDTO();
                                    sysAppMenuDTO1.setId(id);
                                    sysAppMenuDTO1.setUrl("/pages/ReportForm/deliveryRecord/index");
                                    sysAppMenuDTO1.setIcon(sysMenuDTO.getIcon());
                                    sysAppMenuDTO1.setMenuName(sysMenuDTO.getName());
                                    sysAppMenuDTO1.setSort(sysMenuDTO.getSort());
                                    sysAppMenuDTOList1.add(sysAppMenuDTO1);
                                }
                                if(id.equals(1088611641903702208L) && id.equals(sysMenuDTO.getId())){
                                    SysAppMenuDTO sysAppMenuDTO1 = new SysAppMenuDTO();
                                    sysAppMenuDTO1.setId(id);
                                    sysAppMenuDTO1.setUrl("/pages/ReportForm/salesRecord/index");
                                    sysAppMenuDTO1.setIcon(sysMenuDTO.getIcon());
                                    sysAppMenuDTO1.setMenuName(sysMenuDTO.getName());
                                    sysAppMenuDTO1.setSort(sysMenuDTO.getSort());
                                    sysAppMenuDTOList1.add(sysAppMenuDTO1);
                                }

                            });
                        });
                        sysAppMenuDTO.setChildren(sysAppMenuDTOList1);
                    }
                    sysAppMenuDTOList.add(sysAppMenuDTO);
                }
            });
        });

        return sysAppMenuDTOList;
    }

    private List<SysMenuDTO> getUserMenuLists(Long userId,  Long companyId, Long appId, List<Long> roleIds) {
        List<SysMenuEntity> menuList = new ArrayList<>();
        //系统管理员，拥有最高权限
        SysCompanyVO sysCompanyVO = sysCompService.getCompanyInfo(companyId);
        // 获取游客的菜单
        List<SysMenuEntity> guestMenuList = sysGuestMenuDao.getGuestMenuList(MenuTypeEnum.MENU.value(), "zh-CN");
        // 获取公司的菜单
        List<SysMenuEntity> sysMenuEntities;
        if(sysCompanyVO.getAdminUserId().equals(userId)){
            // 公司企业管理员的菜单列表
            sysMenuEntities = sysMenuDao.getMenuListByCompanyId(MenuTypeEnum.MENU.value(), companyId, "zh-CN");
        }else {
            // 公司员工的菜单列表
            sysMenuEntities = sysMenuDao.getUserCompMenuList(companyId, roleIds, MenuTypeEnum.MENU.value(),"zh-CN");
        }
        HashMap<Long, SysMenuEntity> sysMenuEntityHashMap = new HashMap<>(16);
        // 将公司的菜单存放在hashMap中并添加到menuList集合中
        for (SysMenuEntity sysMenuEntity: sysMenuEntities) {
            sysMenuEntityHashMap.put(sysMenuEntity.getId(), sysMenuEntity);
            menuList.add(sysMenuEntity);
        }
        // 将游客的菜单存放到menuList中
        for (SysMenuEntity sysMenuEntity: guestMenuList) {
            if (sysMenuEntityHashMap.get(sysMenuEntity.getId()) == null) {
                menuList.add(sysMenuEntity);
            }
        }
        List<SysMenuDTO> dtoList = ConvertUtils.sourceToTarget(menuList, SysMenuDTO.class);
        // 获取pid为指定AppId
        List<SysMenuDTO> menuDTOList = TreeUtils.build(dtoList);
        if (menuDTOList.size() > 0) {
            menuDTOList = menuDTOList.stream()
                    .filter(sysMenuDTO -> sysMenuDTO.getPid().equals(appId))
                    .collect(Collectors.toList());
        }
        menuDTOList.stream().forEach(item ->{

        });
        return menuDTOList;
    }

    @Override
    public List<SysMenuDTO> getUserMenuList(UserDetail user, Integer type, Long companyId, Long appId, List<Long> roleIds) {
        List<SysMenuEntity> menuList = new ArrayList<>();
        //系统管理员，拥有最高权限
        SysCompanyVO sysCompanyVO = sysCompService.getCompanyInfo(companyId);
        // 获取游客的菜单
        List<SysMenuEntity> guestMenuList = sysGuestMenuDao.getGuestMenuList(MenuTypeEnum.MENU.value(),"zh-CN");
        // 获取公司的菜单
        List<SysMenuEntity> sysMenuEntities;
        if(sysCompanyVO.getAdminUserId().equals(user.getId())){
            // 公司企业管理员的菜单列表
            sysMenuEntities = sysMenuDao.getMenuListByCompanyId(MenuTypeEnum.MENU.value(), companyId,"zh-CN");
        }else {
            // 公司员工的菜单列表
            sysMenuEntities = sysMenuDao.getUserCompMenuList(companyId, roleIds, type,"zh-CN");
        }
        HashMap<Long, SysMenuEntity> sysMenuEntityHashMap = new HashMap<>(16);
        // 将公司的菜单存放在hashMap中并添加到menuList集合中
        for (SysMenuEntity sysMenuEntity: sysMenuEntities) {
            sysMenuEntityHashMap.put(sysMenuEntity.getId(), sysMenuEntity);
            menuList.add(sysMenuEntity);
        }
        // 将游客的菜单存放到menuList中
        for (SysMenuEntity sysMenuEntity: guestMenuList) {
            if (sysMenuEntityHashMap.get(sysMenuEntity.getId()) == null) {
                menuList.add(sysMenuEntity);
            }
        }
        List<SysMenuDTO> dtoList = ConvertUtils.sourceToTarget(menuList, SysMenuDTO.class);
        // 获取pid为指定AppId
        List<SysMenuDTO> menuDTOList = TreeUtils.build(dtoList);
        if (menuDTOList.size() > 0) {
            menuDTOList = menuDTOList.stream()
                    .filter(sysMenuDTO -> sysMenuDTO.getPid().equals(appId))
                    .collect(Collectors.toList());
        }
        return menuDTOList;
    }

    @Override
    public boolean getMenuByType(Long compId,Long appId) {
        List<Long> menuEntities = new ArrayList<>();
        //系统管理员，拥有最高权限
        SysCompanyVO sysCompanyVO = sysCompService.getCompanyInfo(compId);
        // 获取游客的菜单
        List<SysMenuEntity> guestMenuList = sysGuestMenuDao.getGuestMenuList(MenuTypeEnum.MENU.value(), HttpContextUtils.getLanguage());
        // 获取公司的菜单
        List<SysMenuEntity> sysMenuEntities;
        if(sysCompanyVO.getAdminUserId().equals(SecurityUser.getUserId())){
            // 公司企业管理员的菜单列表
            sysMenuEntities = sysMenuDao.getMenuListByCompanyId(MenuTypeEnum.MENU.value(), compId, HttpContextUtils.getLanguage());
        }else {
            List<Long> roleIds = compSysRoleUserService.getRoleId(compId, SecurityUser.getUserId());
            // 公司员工的菜单列表
            sysMenuEntities = sysMenuDao.getUserCompMenuList(compId, roleIds, MenuTypeEnum.MENU.value(),HttpContextUtils.getLanguage());
        }
        HashMap<Long, SysMenuEntity> sysMenuEntityHashMap = new HashMap<>(16);
        // 将公司的菜单存放在hashMap中并添加到menuList集合中
        for (SysMenuEntity sysMenuEntity: sysMenuEntities) {
            menuEntities.add(sysMenuEntity.getId());
        }
        // 将游客的菜单存放到menuList中
        for (SysMenuEntity sysMenuEntity: guestMenuList) {
            menuEntities.add(sysMenuEntity.getId());
        }
        HashSet set = new HashSet(menuEntities);
        menuEntities.clear();
        menuEntities.addAll(set);
        if(menuEntities.contains(appId)){
            return true;
        }
        return false;
    }
}

